package com.medicalfee.util;

import com.medicalfee.dao.ReimbursementList;
import com.medicalfee.dao.StuffInfo;

public class CountReimbursementUtil {
	public static ReimbursementList countReimbursement(
			ReimbursementList reimbursement) {
		StuffInfo stuff = reimbursement.getSid();
		// 可报销额度
		double amount = reimbursement.getAmount();
		String type = reimbursement.getType();
		String level = stuff.getLevel();
		String job = stuff.getJob();
		stuff.getLevel();
		if (stuff.isAircrew() || job.equals("离休")) {
			if (type.equals("门诊")) {
				stuff.setReimbursedByOutpatient(DoubleCalculateUtil.add(amount,
						stuff.getReimbursedByOutpatient()));
			} else if (type.equals("住院")) {
				stuff.setReimbursedByHospitalized(DoubleCalculateUtil.add(
						amount, reimbursement.getAlgorithm()));
			} else {
				amount = DoubleCalculateUtil.mul(
						DoubleCalculateUtil.mul(amount, 0.01),
						reimbursement.getReimbursedRatio());
				reimbursement.setReimbursementAmount(amount);
				return reimbursement;
			}
			reimbursement.setSid(stuff);
			return reimbursement;
		}
		// 已报销门诊额度
		double reimbursedByOutpatient = stuff.getReimbursedByOutpatient();
		// 余额
		double residual = DoubleCalculateUtil.sub(
				stuff.getReimbursementPerYear(), reimbursedByOutpatient);
		if (type.equals("门诊")) {
			if (residual > 0) {
				if (amount <= residual) {
					reimbursement.setReimbursementAmount(amount);
				} else {
					amount = DoubleCalculateUtil.sub(amount, residual);
					if (level.equals("厅级") || job.equals("退休")) {
						if (amount < 2000) {
							amount = DoubleCalculateUtil.mul(amount, 0.77);
						} else if (amount < 5000) {
							amount = DoubleCalculateUtil.add(
									DoubleCalculateUtil.mul(DoubleCalculateUtil
											.sub(amount, 2000), 0.82), 1540);
						} else if (amount < 10000) {
							amount = DoubleCalculateUtil.add(
									DoubleCalculateUtil.mul(DoubleCalculateUtil
											.sub(amount, 5000), 0.87), 4000);
						} else {
							amount = DoubleCalculateUtil.add(
									DoubleCalculateUtil.mul(DoubleCalculateUtil
											.sub(amount, 10000), 0.92), 8350);
						}
					} else if (job.equals("独生子女")) {
						if (level.equals("单职工")) {
							amount = DoubleCalculateUtil.mul(amount, 0.45);
						} else {
							amount = DoubleCalculateUtil.mul(amount, 0.9);
						}
					} else {
						if (amount < 2000) {
							amount = DoubleCalculateUtil.mul(amount, 0.75);
						} else if (amount < 5000) {
							amount = DoubleCalculateUtil.add(
									DoubleCalculateUtil.mul(DoubleCalculateUtil
											.sub(amount, 2000), 0.80), 1500);
						} else if (amount < 10000) {
							amount = DoubleCalculateUtil.add(
									DoubleCalculateUtil.mul(DoubleCalculateUtil
											.sub(amount, 5000), 0.85), 3900);
						} else {
							amount = DoubleCalculateUtil.add(
									DoubleCalculateUtil.mul(DoubleCalculateUtil
											.sub(amount, 10000), 0.90), 8150);
						}
					}
					reimbursement.setReimbursementAmount(DoubleCalculateUtil
							.add(residual, amount));
				}
			} else {
				if (level.equals("厅级") || job.equals("退休")) {
					if (amount < 2000) {
						amount = DoubleCalculateUtil.mul(amount, 0.77);
					} else if (amount < 5000) {
						amount = DoubleCalculateUtil.add(DoubleCalculateUtil
								.mul(DoubleCalculateUtil.sub(amount, 2000),
										0.82), 1540);
					} else if (amount < 10000) {
						amount = DoubleCalculateUtil.add(DoubleCalculateUtil
								.mul(DoubleCalculateUtil.sub(amount, 5000),
										0.87), 4000);
					} else {
						amount = DoubleCalculateUtil.add(DoubleCalculateUtil
								.mul(DoubleCalculateUtil.sub(amount, 10000),
										0.92), 8350);
					}
				} else if (job.equals("独生子女")) {
					if (level.equals("单职工")) {
						amount = DoubleCalculateUtil.mul(amount, 0.45);
					} else {
						amount = DoubleCalculateUtil.mul(amount, 0.9);
					}
				} else {
					if (amount < 2000) {
						amount = DoubleCalculateUtil.mul(amount, 0.75);
					} else if (amount < 5000) {
						amount = DoubleCalculateUtil.add(DoubleCalculateUtil
								.mul(DoubleCalculateUtil.sub(amount, 2000),
										0.80), 1500);
					} else if (amount < 10000) {
						amount = DoubleCalculateUtil.add(DoubleCalculateUtil
								.mul(DoubleCalculateUtil.sub(amount, 5000),
										0.85), 3900);
					} else {
						amount = DoubleCalculateUtil.add(DoubleCalculateUtil
								.mul(DoubleCalculateUtil.sub(amount, 10000),
										0.90), 8150);
					}
				}
				reimbursement.setReimbursementAmount(amount);
			}
			stuff.setReimbursedByOutpatient(DoubleCalculateUtil.add(
					reimbursedByOutpatient, amount));
			reimbursement.setSid(stuff);
			return reimbursement;
		}
		if (type.equals("住院")) {
			if (level.equals("厅级") || job.equals("退休")) {
				if (amount < 2000) {
					amount = DoubleCalculateUtil.mul(amount, 0.93);
				} else if (amount < 5000) {
					amount = DoubleCalculateUtil.add(DoubleCalculateUtil.mul(
							DoubleCalculateUtil.sub(amount, 2000), 0.95), 1860);
				} else if (amount < 10000) {
					amount = DoubleCalculateUtil.add(DoubleCalculateUtil.mul(
							DoubleCalculateUtil.sub(amount, 5000), 0.97), 4710);
				} else {
					amount = DoubleCalculateUtil
							.add(DoubleCalculateUtil.mul(
									DoubleCalculateUtil.sub(amount, 10000),
									0.99), 9560);
				}
			} else if (job.equals("独生子女")) {
				reimbursement.setReimbursementAmount(0);
				return reimbursement;
			} else {
				if (amount < 2000) {
					amount = DoubleCalculateUtil.mul(amount, 0.92);
				} else if (amount < 5000) {
					amount = DoubleCalculateUtil.add(DoubleCalculateUtil.mul(
							DoubleCalculateUtil.sub(amount, 2000), 0.94), 1840);
				} else if (amount < 10000) {
					amount = DoubleCalculateUtil.add(DoubleCalculateUtil.mul(
							DoubleCalculateUtil.sub(amount, 5000), 0.96), 4660);
				} else {
					amount = DoubleCalculateUtil
							.add(DoubleCalculateUtil.mul(
									DoubleCalculateUtil.sub(amount, 10000),
									0.98), 9460);
				}
			}
			long days = (reimbursement.getEndDate().getTime() - reimbursement
					.getStartDate().getTime()) / (24 * 60 * 60 * 1000);
			double algorithm = reimbursement.getAlgorithm();
			double algorithmStandard = DoubleCalculateUtil.mul(
					stuff.getAlgorithmStandard(), days);
			if (algorithm > algorithmStandard) {
				amount = DoubleCalculateUtil.add(amount, algorithmStandard);
			} else {
				amount = DoubleCalculateUtil.add(amount, algorithm);
			}
			reimbursement.setReimbursementAmount(amount);
			stuff.setReimbursedByHospitalized(DoubleCalculateUtil.add(
					stuff.getReimbursedByHospitalized(), amount));
			reimbursement.setSid(stuff);
			return reimbursement;
		}
		if (type.equals("特殊")) {
			amount = DoubleCalculateUtil.mul(
					DoubleCalculateUtil.mul(amount, 0.01),
					reimbursement.getReimbursedRatio());
			reimbursement.setReimbursementAmount(amount);
			return reimbursement;
		}
		return null;
	}
}
